package com.zjh.dao;

import com.zjh.pojo.Department;
import com.zjh.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class EmployeeDao {

    private static Map<Integer, Employee> EmployeeMap = null;

    @Autowired
    private DepartmentDao departmentDao;

    static {
        EmployeeMap = new HashMap<>();

        EmployeeMap.put(1001, new Employee(1001, "AA", "123@qq.com", 1, new Department(101, "部门1")));
        EmployeeMap.put(1002, new Employee(1002, "BB", "123@qq.com", 0, new Department(102, "部门2")));
        EmployeeMap.put(1003, new Employee(1003, "CC", "123@qq.com", 1, new Department(103, "部门3")));
        EmployeeMap.put(1004, new Employee(1004, "DD", "123@qq.com", 0, new Department(104, "部门4")));
        EmployeeMap.put(1005, new Employee(1005, "EE", "123@qq.com", 1, new Department(105, "部门5")));

    }

    //    自增id
    private static int initId = 1006;

    //    增加一个员工
    public void save(Employee employee) {
        if (employee != null) {
            if (employee.getId() == null) {
                employee.setId(initId++);
            }

            employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
            EmployeeMap.put(employee.getId(), employee);
        }
    }

    public Collection<Employee> getEmployees() {
        return EmployeeMap.values();
    }

    public Employee getEmployeeById(Integer id) {
        return EmployeeMap.get(id);
    }

    public void delete(Integer id) {
        EmployeeMap.remove(id);
    }

}
